class Scene {
    constructor(dir) {
        this.dir = dir
        this.imagesLoad = {}
        this.labelLoad = {}
        this.animations = {}
        this.container = {
            'Sprite': this.imagesLoad,
            'Label': this.labelLoad,
            'Animation': this.animations,
        }
        this.type = 'Scene'
    }
    static instance(dir) {
        this.i = this.i || new this(dir)
        return this.i
    }
    mode(type) {
        var map = {
            'update': function (node) {
                node.update()
            },
            'draw': function (node) {
                node.draw()
            }
        }
        var ckeys = Object.keys(this.container)
        for (var i = 0; i < ckeys.length; i++) {
            var cname = ckeys[i]
            var names = Object.keys(this.container[cname])
            for (var j = 0; j < names.length; j++) {
                var name = names[j]
                if (this.container[cname][name] == null) {
                    continue
                }
                map[type](this.container[cname][name])
            }
        }
    }
    update() {
        this.mode('update')
    }

    draw() {
        this.mode('draw')
    }
    addNode(node) {
        this.container[node.type][node.id] = node
    }
    removeNode(node) {
        var reduce = {
            'Sprite': function () {
                Sprite.reduce()
            },
            'Label': function () {
                Label.reduce()
            },
            'Animation': function () {
                Animation.reduce()
            },
        }
        var ckeys = Object.keys(this.container)
        for (var i = 0; i < ckeys.length; i++) {
            var cname = ckeys[i]
            var names = Object.keys(this.container[cname])
            for (var j = 0; j < names.length; j++) {
                var name = names[j]
                if (node.id == name) {
                    this.container[cname][name] = null
                    reduce[node.type]
                }
            }
        }
    }
}